package com.liming.common.util;

import com.liming.common.exception.BusinessException;

import java.util.List;


/**
 * @author liming
 * @date 2025/3/1
 */
public class FeignBeanUtils {

    public static <T, R> R toBean(T feignResult, Class<R> resultClazz) {
        try {
            R result = resultClazz.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(feignResult, result);
            return result;
        } catch (Exception e) {
            throw new BusinessException("类型转换失败");
        }
    }

    public static <T, R> List<R> toBean(List<T> feignResult, Class<R> resultClazz) {
        return feignResult.stream().map(feign -> toBean(feign, resultClazz)).toList();
    }
}